package rest.home.service.services;

import com.mysql.cj.x.protobuf.MysqlxCrud;
import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager;
import rest.home.coontuil.ConnUtil;
import rest.home.dao.dao.RestHomeDao;
import rest.home.entity.*;
import rest.home.entity.ms.Limit;
import rest.home.entity.ms.OlderAndVisitorAndRoom;
import rest.home.service.RestHomeSer;

import java.net.CookieHandler;
import java.sql.SQLException;
import java.util.List;

/**
 * @author: lbc
 * @Date: 2022/5/5 15:26
 * @version: 8.0
 * @describe:
 */
public class RestHomeService implements RestHomeSer {
    RestHomeDao rhd=new RestHomeDao();
    //登录
    @Override
    public List<Admin> login() {
        List<Admin> list=null;
        try {
            list = rhd.login();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return list;
    }

    //新增账号密码
    @Override
    public Boolean addAdmin(Admin admin) {
        Boolean f=false;
        try {
            Integer addA = rhd.addAdmin(admin);
            if (addA!=null||addA!=0){
                f= true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return f;
    }

    //2.1、查询老人信息
    @Override
    public List<Older> findAll() {
        List<Older> list=null;
        try {
            list = rhd.findAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return list;
    }

    //2.2、修改老人信息
    @Override
    public Integer updateOlder(Older older) {
        Integer i=null;
        try {
            i = rhd.updateOlder(older);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return i;
    }


    //展示所有的房间信息
    @Override
    public List<Room> showRoom() {
        List<Room> rooms = null;
        try {
            rooms = rhd.showRoom();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return rooms;
    }
    //展示可用的房间信息
    @Override
    public List<Room> showRoomByStatus() {
        List<Room> rooms = null;
        try {
            rooms = rhd.showRoomByStatus();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return rooms;
    }
    //修改一条老人的入住信息
    @Override
    public Integer updateRoomMessage(Room room) {
        Integer message = null;
        try {
            message = rhd.updateRoomMessage(room);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return message;
    }
    //添加一条老人的入住信息
    @Override
    public Integer addRoomMessage(Room room) {
        Integer addRoomMessage = null;
        try {
            addRoomMessage = rhd.addRoomMessage(room);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return addRoomMessage;
    }

    @Override
    public Boolean deleteRoomMessage(Integer id) {
        boolean f=false;
        Integer deleteFlag = null;
        try {
            deleteFlag = rhd.deleteRoomMessage(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (deleteFlag!=null||deleteFlag!=0){
            f=true;
        }
        return f;
    }

    @Override
    public List<Room> showRoomByType(String typeName) {
        List<Room> rooms = null;
        try {
            rooms = rhd.showRoomByType(typeName);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return rooms;
    }

    public List<Trouble> findAllTrouble(){
        List<Trouble> ab =null;
        try {
            ab = rhd.findAllTrouble();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return ab;
    }


    //根据老人ID查询老人信息
    @Override
    public Older findOlderIdByOlder(Integer olderId) {
        Older olderIdByOlder=null;
        try {
            olderIdByOlder = rhd.findOlderIdByOlder(olderId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return olderIdByOlder;
    }
    //修改事故信息
    public Integer updateTroubleById(Trouble trouble){
        Integer tbId =null;
        try {
            tbId = rhd.updateTroubleById(trouble);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return tbId;
    }
    public Trouble findTroubleById(Integer troubleId){
        Trouble troubleById =null;
        try {
            troubleById = rhd.findTroubleById(troubleId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return troubleById;
    }
    //新增事故信息
    public Boolean addTrouble(Trouble trouble){
        Integer addTb=null;
        try {
            addTb = rhd.addTrouble(trouble);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }if (addTb==null||addTb==0){
            return false;
        }
        return true;
    }
    //删除事故信息
    public Boolean deleteTrouble(Integer troubleId){
        Integer deleteTb =null;
        try {
            deleteTb = rhd.deleteTrouble(troubleId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }if (deleteTb==null||deleteTb==0){
            return false;
        }
        return true;
    }

    //展示缴费信息
    @Override
    public List<PayMent> findAllPayMent() {
        List<PayMent> allPayMent =null;
        try {
            allPayMent = rhd.findAllPayMent();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return allPayMent;
    }

    @Override
    public List<Nurse> findAllNurse() {
        List<Nurse> allNurse =null;
        try {
            allNurse = rhd.findAllNurse();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return allNurse;
    }

    //判断护理人员是否存在
    @Override
    public Nurse existsNurseId(Integer integer) {
        Nurse nurse =null;
        try {
            nurse = rhd.existsNurseId(integer);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        if (nurse!=null){
            return nurse;
        }
        return null;
    }

    //修改护理人员信息
    @Override
    public Boolean updateNurse(Nurse nurse) {
        Integer integer =null;
        try {
            integer = rhd.updateNurse(nurse);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        if (integer!=null||integer!=0){
            return true;
        }
        return false;
    }

    @Override
    public boolean addNurse(Nurse nurse) {
        Integer integer =null;
        try {
            integer = rhd.addNurse(nurse);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        if (integer!=null||integer!=0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteNurse(Integer integer) {
        Integer delete =null;
        try {
            delete = rhd.deleteNurse(integer);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        if (delete!=null||delete!=0){
            return true;
        }
        return false;
    }

    //添加老人
    @Override
    public Boolean addOlder(Older older) {
        Boolean f=false;
        try {
            Integer addOlder = rhd.addOlder(older);
            if (addOlder!=null||addOlder!=0){
                f= true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return f;
    }

    //删除老人
    @Override
    public Boolean deleteOlder(Integer deleteOlderId) {
        Integer deleteOlder=null;
        try {
            deleteOlder = rhd.deleteOlder(deleteOlderId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        if (deleteOlder==null || deleteOlder==0){
            return false;
        }
        return true;
    }

    @Override
    public List<Health> showHealthByType() {
        List<Health> list = null;
        try {
            list = rhd.showHealthByType();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return list;

    }

    @Override
    public Boolean updateHealthMessage(Health health) {
        Integer healthMessage = null;
        try {
            healthMessage = rhd.updateHealthMessage(health);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        Boolean healthFlag= false;
        if (healthMessage!=null||healthMessage!=0){
            healthFlag=true;
        }
        return healthFlag;
    }

    @Override
    public List<Medicine> showMedicine() {
        List<Medicine> medicines = null;
        try {
            medicines = rhd.showMedicine();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return medicines;
    }

    @Override
    public Boolean addMedicine(Medicine medicine) {
        Integer addMedicine=null;
        try {
            addMedicine= rhd.addMedicine(medicine);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        Boolean addMedicineFlag=false;
        if (addMedicine!=null||addMedicine!=0){
            addMedicineFlag=true;
        }
        return addMedicineFlag;
    }

    @Override
    public Boolean updateMedicine(Medicine medicine){
        Integer updateMedicine=null;
        try {
            updateMedicine  = rhd.updateMedicine(medicine);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        if (updateMedicine!=null||updateMedicine!=0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean deleteMedicine(Integer integer) {
        Integer deleteMedicine=null;
        try {
            deleteMedicine = rhd.deleteMedicine(integer);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        if (deleteMedicine!=null||deleteMedicine!=0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean addHealth(Health health) {
        Integer addHealth=null;
        try {
            addHealth = rhd.addHealth(health);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        if (addHealth!=null||addHealth!=0){
            return true;
        }
        return false;
    }

    @Override
    public List<NursingPayMent> showAllNursing() {
        List<NursingPayMent> nursingPayMents =null;
        try {
            nursingPayMents = rhd.showAllNursing();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return nursingPayMents;
    }
    public List<Visitor> showAllVisitor() {
        List<Visitor> vs =null;
        try {
            vs = rhd.showAllVisitor();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return vs;
    }
    public Visitor findVisitorById(Integer vistorId){
        Visitor vistorById =null;
        try {
            vistorById = rhd.findVisitorById(vistorId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return vistorById;
    }
    public Boolean updateVisitore(Visitor visitor){
        Integer updateVs =null;
        try {
            updateVs = rhd.updateVisitore(visitor);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }if (updateVs!=null||updateVs!=0){
            return true;
        }
        return false;
    }
    public Boolean addVisitor(Visitor visitor){
        Integer addVs =null;
        try {
            addVs = rhd.addVisitor(visitor);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        if (addVs!=null||addVs!=0){
            return true;
        }
        return false;
    }
    public Boolean deleteVisitor(Integer visitorId){
        Integer deleteVs =null;
        try {
            deleteVs = rhd.deleteVisitor(visitorId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        if (deleteVs!=null||deleteVs!=0){
            return true;
        }
        return false;
    }

    //查询缴费状态
    @Override
    public List<PayMent> findOlderByStatus(Integer olderId) {
        List<PayMent> olderByStatus = null;
        try {
            olderByStatus = rhd.findOlderByStatus(olderId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return olderByStatus;
    }

    //查看外出信息
    @Override
    public List<Outing> showAllOuting() {
        List<Outing> list=null;
        try {
            list= rhd.showAllOuting();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return list;
    }

    //外出报备
    @Override
    public Boolean addOuting(Outing outing) {
        Integer addOut=null;
        try {
            addOut = rhd.addOuting(outing);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        if (addOut!=null||addOut!=0){
            return true;
        }
        return false;
    }

    //判断外出报备ID是否存在
    @Override
    public Outing existsOutingId(Integer outingId) {
        Outing outing=null;
        try {
            outing = rhd.existsOutingId(outingId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        if (outing==null){
            return null;
        }
        return outing;
    }

    //外出归来修改报备状态
    @Override
    public Boolean updateOuting(Outing outing) {
        Integer updateOut=null;
        try {
            updateOut = rhd.updateOuting(outing);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        if (updateOut!=null||updateOut!=0){
            return true;
        }
        return false;
    }
    //分页展示访客信息及老人信息及房间号
    public Limit<OlderAndVisitorAndRoom> OlderAndVisitorAndRoomByPage(Integer index){
        if (index==null||index==0){
            index=1;
        }
        Limit<OlderAndVisitorAndRoom> limit = new Limit<>();
        limit.setOlderLocal(index);
        Integer olderNum =null;
        try {
            olderNum = rhd.findOlderNum();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        if (olderNum!=null){
            limit.setOlderNums(olderNum);
            if (olderNum%2==0){
                limit.setOlderPage(olderNum/2);
            }else {
                limit.setOlderPage(olderNum/2+1);
            }
        }
        List<OlderAndVisitorAndRoom> olderAndVisitorAndRooms =null;
        try {
            olderAndVisitorAndRooms = rhd.OlderAndVisitorAndRoomByPage(index*2-2);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        limit.setList(olderAndVisitorAndRooms);
        return limit;
    }
}
